import 'package:flutter_bili_app/http/const.dart';
import 'package:flutter_bili_app/http/request/http_method.dart';

class BaseRequest {
  var pathParams;
  var useHttps = Const.useHttps;
  HttpMethod httpMethod;
  String pathUrl;

  Map<String, Object>? fromData;
  Map<String, Object> header = {};
  Object? data;

  Map<String, dynamic> queryParameters = {};

  BaseRequest(
      {this.httpMethod = HttpMethod.GET,
      this.pathUrl = "",
      this.data,
      this.fromData});

  Map<String, Object> getAllHeader() {
    commonHeader();
    return header;
  }

  Map<String, Object>? getFromData() {
    commonParameters();
    return fromData;
  }

  String authority() {
    return Const.BaseUrl;
  }

  HttpMethod getHttpMethod() {
    return httpMethod;
  }

  String path() {
    return pathUrl;
  }

  BaseRequest setHttpMethod(HttpMethod httpMethod) {
    this.httpMethod = httpMethod;
    return this;
  }

  BaseRequest setPath(String pathurl) {
    this.pathUrl = pathurl;
    return this;
  }

  String requestUrl() {
    var pathUrl = path();
    /* if (authority().endsWith("/")) {
      pathUrl = authority() + path();
    } else {
      pathUrl = authority() + "/" + path();
    }*/
    Uri uri;
    if (useHttps) {
      uri = Uri.https(authority(), pathUrl, pathParams);
    } else {
      uri = Uri.http(authority(), pathUrl, pathParams);
    }
    return uri.toString();
  }

  BaseRequest addFromData(String key, Object value) {
    fromData ??= {};
    fromData?[key] = value.toString();
    return this;
  }

  BaseRequest removeFromData(String key) {
    fromData?.remove(key);
    return this;
  }

  BaseRequest addQueryParameters(String key, Object value) {
    queryParameters?[key] = value;
    return this;
  }

  BaseRequest removeQueryParameters(String key) {
    queryParameters?.remove(key);
    return this;
  }

  BaseRequest addHeader(String key, Object value) {
    header[key] = value.toString();
    return this;
  }

  BaseRequest removeHeader(String key) {
    header.remove(key);
    return this;
  }

  BaseRequest commonHeader() {
    Map<String, Object> map = {};
    map['testheader'] = 'testheader';
    header.addAll(map);
    return this;
  }

  BaseRequest commonParameters() {
    Map<String, Object> map = {};
    map['testcommParameters'] = 'testcommParameters';
    fromData?.addAll(map);
    return this;
  }
}
